package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@Controller
public class fileControleller {
    @RequestMapping("Upload")
    public String Upload(HttpServletRequest req, HttpServletResponse resp,
                         @RequestParam("discription") String discription, @RequestParam("multipartFile") MultipartFile multipartFile, Model model) throws IOException {
        //上传文件的地址
        String path = req.getServletContext().getRealPath("/image/");
        //上传文件的文件名
        String filename = multipartFile.getOriginalFilename();
        //创建一个新的文件
        File file = new File(path, filename);
        //判断我上传文件的地址的文件夹是否存在
        if (!file.getParentFile().exists()){
            //如果文件夹不存在，就创建一个新的文件夹
            file.getParentFile().mkdir();
        }
        //将文件复制到指定文件夹
        multipartFile.transferTo(new File(path+File.separator+filename));

        model.addAttribute("msg","插入文件夹的路径为"+new File(path+File.separator+filename));
        return"/WEB-INF/success.jsp";
    }
}
